package com.xxx.delayjob.listener;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.xxx.delayjob.util.JodaUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.sql.Timestamp;
import java.util.Date;

/**
 * 系统Listener
 * ApplicationListener在springboot启动之后执行
 */
@Log4j2
public class SystemListener implements ApplicationListener<ContextRefreshedEvent> {

	public static String versionNo;  // 系统启动时间戳，作为静态资源版本号
	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
        versionNo = JodaUtil.getVersionNo();
        
		log.info("开始设定fastjson全局转换策略");
		SerializeConfig serializeConfig = SerializeConfig.globalInstance;
		serializeConfig.put(Long.class, ToStringSerializer.instance);
		serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
		serializeConfig.put(Timestamp.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
		serializeConfig.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
	}
	
}
